<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 * 团购
 * 
 */
namespace osc\common\service;
use think\Db;
class Tuan{
	
	//取得首页团购商品（电脑版本）
	public function get_home_tuangou_goods(){
		
		if (!$home_tuangou= cache('home_tuangou')) {
		
			$home_tuangou=Db::name('group_buy')->where('status',1)->order('views desc')->limit(6)->select();
		
			cache('home_tuangou', $home_tuangou);
			
		}
		
		if(empty($home_tuangou)){
			return null;
		}
		
		return $home_tuangou;
	}
	//团购详情
	public function get_tuan_info(){
		
		if(!$tuan=Db::name('group_buy')->find((int)input('param.id'))){
			return false;
		}
		//使用关联商品的图片详情
		if($tuan['use_goods_des']){
			$image=Db::name('goods_mobile_description_image')->where('goods_id',$tuan['goods_id'])->order('sort_order asc')->select();
		}else{
			$image=Db::name('group_buy_images')->where('group_buy_id',(int)input('param.id'))->select();
		}
		
		return array(
			'tuan'=>$tuan,
			'image'=>$image,
			'options'=>osc_goods()->get_goods_options($tuan['goods_id'])
		);		
	}
	//检查限购数量
	public function check_buy_limit($group_buy_id,$uid){
		$tuan=Db::name('group_buy')->where('status',1)->find((int)$group_buy_id);
		
		if(!$tuan){
			return array('error'=>'团购商品不存在');
		}		
		//判断团购时间
		if(time()<$tuan['start_time']){
			return array('error'=>'团购未开始');
		}
		if(time()>$tuan['end_time']){
			return array('error'=>'团购已经结束');
		}
		
		if($tuan['buy_limit']==0){//不限购，可以购买
			return true;
		}else{
			$num=Db::name('order')->where(array('uid'=>$uid,'group_buy_id'=>$group_buy_id,'order_status_id'=>config('paid_order_status_id')))->count();
			if($num>=$tuan['buy_limit']){				
				return array('error'=>'您已经达到了购买上限');
			}
		}
		
	}

	
	//团购加入订单
	//$id cart表主键，$address_id 收货地址，$payment_method 支付方式
	function add_order($id,$address_id,$payment_method,$comment=''){
		//收货地址
		$shipping=$address_id;
		//支付方式
		$payment=$payment_method;
		
		if(empty($shipping)){
			return array('error'=>'请选择收货地址');
		}

		if(empty($payment)){
			return array('error'=>'请选择支付方式');
		}
		
		if(!member('uid')){
			return array('error'=>'请先登录');
		}
		
		$id=hashids_decode($id);
		
		if(!$id){		
			return array('error'=>'非法操作');
		}

		$cart=Db::name('cart')->where('cart_id',$id)->find();	
		
		$goods_option=osc_cart()->get_cart_options($cart['goods_id'],json_decode($cart['goods_option']));
		
		$product=Db::name('group_buy')->where('id',$cart['group_buy_id'])->find();	
		
		$address=Db::name('address')->find($shipping);
		//订单数据
		$order['uid']=member('uid');	
		$order['group_buy_id']=$product['id'];	
		$order['type']=config('tuan_goods');		
		$order['order_num_alias']=build_order_no();
		$order['name']=member('username');		
		$order['email']=member('email');
		$order['tel']=member('username');		
		$order['shipping_name']=$address['name'];				
		$order['shipping_city_id']=$address['city_id'];		
		$order['shipping_country_id']=$address['country_id'];
		$order['shipping_province_id']=$address['province_id'];
		$order['shipping_tel']=$address['telephone'];	
		$order['shipping_method']=config('default_transport_id');
		$order['address_id']=$shipping;		
		$order['order_status_id']=config('default_order_status_id');		
		$order['ip']=get_client_ip();		
		$order['date_added'] =time();
		$order['total'] =$product['group_price'];
		$order['user_agent']=$_SERVER['HTTP_USER_AGENT'];
		$order['payment_code']=$payment_method;
		$order['pay_subject']=$product['group_name'];
		$order['free_shipping']=1;//免运费	
		$order['comment']=empty($comment)?'':$comment;
			
		$order_id=Db::name('Order')->insert($order,false,true);	
		//商品
		$goods['order_id']=$order_id;
		$goods['order_num_alias']=$order['order_num_alias'];
		$goods['goods_id']=$product['id'];
		$goods['name']=$product['group_name'];
		$goods['image']=$product['group_image'];
		$goods['type']=2;
		$goods['quantity']=1;
		$goods['price']=$product['group_price'];
		$goods['total']=$product['group_price'];		
		$goods['uid']=$order['uid'];
		$goods['order_status_id']=$order['order_status_id'];
		
		$order_goods_id=Db::name('order_goods')->insert($goods,false,true);	
		//商品选项
		if(!empty($goods_option)){		
			foreach ($goods_option as $k => $v) {
				Db::name('order_option')->insert(array(
				'order_id'=>$order_id,
				'goods_id'=>$product['goods_id'],
				'order_goods_id'=>$order_goods_id,
				'option_id'=>$v['option_id'],
				'option_value_id'=>$v['option_value_id'],				
				'name'=>$v['name'],
				'value'=>$v['value'],
				'type'=>$v['type']				
				),false,true);	
			}
		}

		$totals[0]=array(
			'code'=>'sub_total',
			'title'=>'商品价格',
			'text'=>'￥'.$product['group_price'],
			'value'=>$product['group_price']				
			);
		$totals[1]=array(
			'code'=>'shipping',
			'title'=>'运费',
			'text'=>'￥ 0',
			'value'=>0				
		);				
		$totals[2]=array(
			'code'=>'total',
			'title'=>'总价',
			'text'=>'￥'.$product['group_price'],
			'value'=>$product['group_price']			
		);
		
		foreach ($totals as $k => $v) {
				Db::name('order_total')->insert(array(
				'order_id'=>$order_id,
				'code'=>$v['code'],
				'title'=>$v['title'],
				'text'=>$v['text'],
				'value'=>$v['value']							
				),false,true);	
		}
		
		$oh['order_id']=$order_id;		
		$oh['order_status_id']=config('default_order_status_id');		
		$oh['date_added']=time();
		$oh_id=Db::name('OrderHistory')->insert($oh);
		
		return array(
			'cart_id'=>$id,
			'order_id'=>$order_id,
			'subject'=>$order['pay_subject'],			
			'name'=>$order['shipping_name'],//收货人姓名
			'pay_order_no'=>$order['order_num_alias'],
			'order_num_alias'=>$order['order_num_alias'],
			'pay_total'=>$order['total'],
			'total'=>$order['total'],
			'username'=>$order['name'],
			'uid'=>$order['uid']
		);
	}
	
	public function update_buyer_count($group_buy_id){
		Db::name('group_buy')->where('id',$group_buy_id)->setInc('buyer_count',1);	
	}
	public function clear_cart($uid){
		Db::name('cart')->where(array('uid'=>$uid,'type'=>config('tuan_goods')))->delete();
	}
}  